package com.cndw.kungfu.model.goods.app;

import java.util.Map;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.LogServer;
/**
 * 丹药的使用
 */
public class AppDo152 extends AppBase {

	@Override
	public boolean check() throws Exception {
		if(num <= 0){
			return false;
		}
		int takeNum = player.getDataInfo().getDrugNum(goodsMode.getGoodsId());
		int drugStage = 0;// 丹药等级 0初级 1中级 2高级
		for (int i = 0; i < ConstantsLevel.lv30Drug.length; i++) {
			if (goodsMode.getPlayerLevel() >= ConstantsLevel.lv30Drug[i]) {
				drugStage = i;
			}
		}
		if (takeNum + num > ConstantsModel.drugTakeNumLimit[drugStage]) {
			//玩家嗑药次数达到上限
			throw new ErrPopException(LanguageLib.get("drugs.takedrug.takenumlimit", ConstantsModel.drugTakeNumLimit[drugStage]));			
		}
		return true;
	}

	@Override
	public Map<String, Object> run() throws Exception {
		int drugType = goodsMode.getValueA();
		int attriAdd = goodsMode.getValueB() * num;
		int takeAmount = player.getDataInfo().increaseDrugNum(goodsMode.getGoodsId(), num);
		player.getDataInfo().increaseDrugsAdd(drugType, attriAdd);
		player.getDataInfo().commitSync();
		Context.getPlayerServer().clacProperties(player);
		player.commitSync();
		msg.put("player", player.toPlayerVO());
		LogServer.recordLog(player, ConstantsLog.player, 30007, goodsMode.getName(), num, attriAdd, takeAmount);
		return msg;
	}

}
